package base.copy;

/**
 * @author liweimin
 * @date 2021/5/29 11:21
 * @since
 */
public class DeepCopy1 {
    public static void main(String[] args) {
        Age age = new Age(35);
        Student student = new Student("lwm", age, 175);

        Student student1 = (Student) student.clone();
        System.out.println(student1 == student);//false
        System.out.println(student.getClass() == student1.getClass());//true
        System.out.println(student.equals(student1));//不强制要求

        System.out.println(student);
        System.out.println(student1);
        System.out.println("======================== 改变student的属性值 ========================");

        age.setAge(18);
        student.setName("李二狗");
        student.setLength(180);

        System.out.println(student);
        System.out.println(student1);
    }

    static class Student implements Cloneable {


        //学生类的成员变量（属性）,其中一个属性为类的对象
        private String name;
        private Age aage;
        private int length;

        //构造方法,其中一个参数为另一个类的对象
        public Student(String name, Age a, int length) {
            this.name = name;
            this.aage = a;
            this.length = length;
        }


        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Age getaAge() {
            return this.aage;
        }

        public void setaAge(Age age) {
            this.aage = age;
        }

        public int getLength() {
            return this.length;
        }

        public void setLength(int length) {
            this.length = length;
        }

        //设置输出的字符串形式
        @Override
        public String toString() {
            return "姓名是： " + this.getName() + "， 年龄为： " + this.getaAge().toString() + ", 长度是： " + this.getLength();
        }

        //重写clone方法
        @Override
        public Object clone() {
            Object obj = null;
            //调用Object类的clone方法，返回一个Object实例
            try {
                obj = super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            Student student = (Student) obj;
            //把Age的属性也拷贝了
            student.aage = (Age) student.getaAge().clone();
            return obj;
        }
    }

    static class Age implements Cloneable {
        //年龄类的成员变量（属性）
        private int age;

        //构造方法
        public Age(int age) {
            this.age = age;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return this.age + "";
        }

        @Override
        public Object clone() {
            Object obj = null;
            try {
                obj = super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return obj;
        }
    }
}
